var express = require('express');
var router = express.Router();

const { MedModel, DoctorModel, HaoModel } = require('../model/model');

const Core = require('@alicloud/sms-sdk')

//数据
router.get('/medData', async (req, res) => {
  let { pageNow, pageSize } = req.query
  // console.log(pageNow, pageSize);


  let data = await MedModel.find()


  let total = data.length
  // console.log(total);

  let index = data.slice((pageNow - 1) * pageSize, pageNow * pageSize)
  let pagelist = Math.ceil(total / pageSize)
  // console.log(index);

  res.send({
    code: 200,
    msg: "查询成功",
    total,
    pagelist,
    index
    // data
  })
});


router.post('/add', (req, res) => {
  MedModel.create(req.body)
  res.send({
    code: 200,
    msg: '添加成功',
  })
})

//删除
router.post('/delete', async (req, res) => {
  let { id } = req.body
  console.log(id);

  await MedModel.deleteOne({ _id: id })
  res.send({
    code: 204,
    msg: '删除成功'
  })
})

//修改
router.post('/edit', async (req, res) => {
  let body = req.body
  console.log(body);

  let id = body.id
  await MedModel.updateOne({ id }, body)
  res.send({
    code: 203,
    msg: '修改成功'
  })
})

//医生添加
router.post('/att', (req, res) => {
  DoctorModel.create(req.body)
  res.send({
    code: 200,
    msg: '添加成功',
  })
})

router.get('/doclist', async (req, res) => {
  let list = await DoctorModel.find()
  res.send({
    code: 200,
    msg: '添加成功',
    list
  })
})


//号源
// router.get('/haolist', async (req, res) => {
//   let stu = await HaoModel.find()
//   res.send({
//     code: 200,
//     msg: '添加成功',
//     stu
//   })
// })

router.get('/haolist', async (req, res) => {
  let { pages, pageSize } = req.query
  let data = await HaoModel.find()
  console.log(data);
  let total = data.length
  let stu = data.slice((pages - 1) * pageSize, pages * pageSize)
  let pagelist = Math.ceil(total / pageSize)

  res.send({
    code: 200,
    msg: "查询成功",
    total,
    pagelist,
    stu
  })
});


router.post('/haoadd', (req, res) => {
  HaoModel.create(req.body)
  res.send({
    code: 200,
    msg: '添加成功',
  })
})

//删除
router.post('/delete/:id', async (req, res) => {
  let { id } = req.params
  console.log(id);

  await HaoModel.deleteOne({ id })
  res.send({
    code: 204,
    msg: '删除成功'
  })
})

//修改
router.post('/edit', async (req, res) => {
  let body = req.body
  console.log(body);

  let id = body.id
  await HaoModel.updateOne({ id }, body)
  res.send({
    code: 203,
    msg: '修改成功'
  })
})


module.exports = router;